/*
 * Filename DeviceAppMapping.java 2022年04月19日
 * Copyright © Ehome Co.Ltd. All Rgiths Reserved.
 * @author wuyingchao
 */
package com.ehome.poct.terminal.mapping;

import com.ehome.poct.base.api.dto.device.TestDeviceDTO;
import com.ehome.poct.base.api.dto.device.TestDeviceQrcodePrintHisDTO;
import com.ehome.poct.base.api.dto.device.bootModule.BootInfoDTO;
import com.ehome.poct.base.api.dto.device.bootModule.TestDeviceBootInfoDTO;
import com.ehome.poct.base.api.dto.device.bootModule.TestDeviceBootingDetailDTO;
import com.ehome.poct.base.api.dto.device.bootModule.TestDeviceBootingHistDTO;
import com.ehome.poct.base.api.dto.sys.MqttInfoDTO;
import com.ehome.poct.terminal.api.constant.MqttTopic;
import com.ehome.poct.terminal.api.dto.QrCodeHisDTO;
import com.ehome.poct.terminal.api.vo.DeviceExtInfoVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.time.LocalDateTime;

/**
 * @author wyc
 * @since 2022/04/19 14:38
 **/
@Mapper(componentModel = "spring", imports = {MqttTopic.class})
public interface DeviceAppMapping {
    @Mapping(target = "deviceSno", source = "device.sno")
    @Mapping(target = "deviceVersion", source = "device.version")
    @Mapping(target = "publicTopic", expression = "java(MqttTopic.PUBLIC.getPrefix())")
    @Mapping(target = "deviceTopic", expression = "java(MqttTopic.DEVICE.getPrefix() + device.getSno())")
    @Mapping(target = "clinicTopic", expression = "java(MqttTopic.CLINIC.getPrefix() + device.getTermClinicId())")
    @Mapping(target = "mqttUrl", source = "mqtt.url")
    DeviceExtInfoVO to(TestDeviceDTO device, MqttInfoDTO mqtt, Boolean needPay, Boolean onlineAble,String htmlDomain,Integer limitRight);


    TestDeviceQrcodePrintHisDTO to(QrCodeHisDTO dto);

    @Mappings({
            @Mapping(target = "deviceType",constant= "2"),
            @Mapping(target = "checkResult",expression = "java(resultToInt(dto.getCheck().getPass()))" ),
            @Mapping(target = "communicationResult",expression = "java(resultToInt(dto.getCommunication().getPass()))"),
            @Mapping(target = "motionResult",expression = "java(resultToInt(dto.getMotion().getPass()))"),
            @Mapping(target = "networkResult",expression = "java(resultToInt(dto.getNetwork().getPass()))"),
            @Mapping(target = "storageResult",expression = "java(resultToInt(dto.getScanner().getPass()))"),
            @Mapping(target = "scannerResult",expression = "java(resultToInt(dto.getStorage().getPass()))"),
            @Mapping(target = "bootingCheckTime",source = "checkTime"),
            @Mapping(target = "testDeviceBootingHistId",source= "hisId")
    })
    TestDeviceBootingHistDTO toHist(TestDeviceBootInfoDTO dto, String sno, Long hisId, LocalDateTime checkTime);

    @Mappings({
            @Mapping(target = "testDeviceBootingHistId",source= "hisId"),
            @Mapping(target = "resultJson",source= "bootingJson"),
            @Mapping(target = "sno",source= "sno"),
    })
    TestDeviceBootingDetailDTO toDetail(TestDeviceBootInfoDTO dto,String bootingJson,String sno,Long hisId);

    @Mappings({
            @Mapping(target = "histDTO",source = "histDTO"),
            @Mapping(target = "detailDTO",source = "detailDTO"),
    })
    BootInfoDTO toBootInfo(TestDeviceBootingDetailDTO detailDTO,TestDeviceBootingHistDTO histDTO);

    default Integer resultToInt(Boolean result){
        return Boolean.TRUE.equals(result)?1:0;
    }
}